package com.utils;

import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/***
 * 该类用于登录的拦截认证
 */
@Controller
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, @NotNull HttpServletResponse response, @NotNull Object handler) throws Exception {
        HttpSession session = request.getSession();
        if (session.getAttribute("admin") != null) {
            // 登录成功不拦截
            return true;
        } else {
            // 拦截后进入登录页面
            response.sendRedirect(request.getContextPath() + "/toLogin");
            return false;
        }
    }
}
